package org.dreamfly.core.service.impl;

import org.dreamfly.core.action.FileActionSupport;
import org.dreamfly.core.bean.SzModel;
import org.dreamfly.core.bean.SzModelType;
import org.dreamfly.core.dao.ModelDAO;
import org.dreamfly.core.dao.ModelTypeDAO;
import org.dreamfly.core.page.PageInfo;
import org.dreamfly.core.page.PageResultSet;
import org.dreamfly.core.service.ModelService;
import org.dreamfly.core.util.UrlGetter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Gin
 * Date: 2010-10-10
 * Time: 0:58:51
 * To change this template use File | Settings | File Templates.
 */
public class ModelServiceImpl implements ModelService {
    private ModelDAO modelDAO;
    private ModelTypeDAO modelTypeDAO;
    private UrlGetter urlGetter;

    @Override
    public void saveOrUpdate(SzModel model) {
        modelDAO.saveOrUpdate(model);
    }

    public void setModelDAO(ModelDAO modelDAO) {
        this.modelDAO = modelDAO;
    }

    public void setModelTypeDAO(ModelTypeDAO modelTypeDAO) {
        this.modelTypeDAO = modelTypeDAO;
    }

    @Override
    public PageResultSet<SzModel> normalSearch(SzModel model, Long pageSize, Long page, String serverName) {
        PageInfo pageInfo = null;
        List<SzModel> list = null;
        if (null == serverName) {
            pageInfo = new PageInfo(modelDAO.findCount(SzModel.class, model.getTitle()), pageSize, page);
            list = modelDAO.find(SzModel.class,
                    model.getTitle(),
                    Integer.parseInt(pageInfo.getBeginIndex().toString()),
                    Integer.parseInt(pageInfo.getPageSize().toString()), null);
        } else {
            pageInfo = new PageInfo(modelDAO.findNativeCount(SzModel.class, model.getTitle(), serverName), pageSize, page);
            list = modelDAO.findNative(SzModel.class,
                    model.getTitle(),
                    serverName,
                    Integer.parseInt(pageInfo.getBeginIndex().toString()),
                    Integer.parseInt(pageInfo.getPageSize().toString()));
        }
        PageResultSet<SzModel> pageResultSet = new PageResultSet<SzModel>();
        pageResultSet.setList(list);
        pageResultSet.setPageInfo(pageInfo);
        return pageResultSet;
    }

    @Override
    public PageResultSet<SzModel> normalSearchByType(List<Long> typeIds, SzModel model,
                                                     Long pageSize, Long page, String serverName) {
        PageInfo pageInfo;
        List<SzModel> list;
        if (null == typeIds) {
            typeIds = new ArrayList<Long>();
            List<SzModelType> types = modelTypeDAO.findAll(SzModelType.class);
            for (SzModelType type : types) {
                typeIds.add(type.getTypeId());
            }
        }
        if (null == serverName) {
            pageInfo = new PageInfo(modelDAO.findCount(typeIds, model.getTitle(), null), pageSize, page);
            list = modelDAO.findByType(typeIds,
                    model.getTitle(),
                    Integer.parseInt(pageInfo.getBeginIndex().toString()),
                    Integer.parseInt(pageInfo.getPageSize().toString()), null);
        } else {
            pageInfo = new PageInfo(modelDAO.findCount(typeIds, model.getTitle(), serverName), pageSize, page);
            list = modelDAO.findByType(typeIds,
                    model.getTitle(),
                    Integer.parseInt(pageInfo.getBeginIndex().toString()),
                    Integer.parseInt(pageInfo.getPageSize().toString()), serverName);
        }
        if (null != list) {
            Iterator iterator = list.iterator();
            while (iterator.hasNext()) {
                SzModel temp = (SzModel) iterator.next();
                temp.setMinPictureUrl(urlGetter.getRemoteUrl(temp.getMinPictureUrl(), temp.getServer()));
            }
        }
        PageResultSet<SzModel> pageResultSet = new PageResultSet<SzModel>();
        pageResultSet.setList(list);
        pageResultSet.setPageInfo(pageInfo);
        return pageResultSet;
    }

    @Override
    public void delete(Long id) {
        SzModel m = modelDAO.find(SzModel.class, id);
        FileActionSupport fas = new FileActionSupport();
        String[] filePaths = {m.getModelUrl(), m.getMinPictureUrl()};
        fas.setFilePaths(filePaths);
        try {
            fas.delete();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        modelDAO.delete(m);
    }

    @Override
    public SzModel findDetail(Long id) {
        SzModel model = modelDAO.find(SzModel.class, id);
        model.setMinPictureUrl(urlGetter.getRemoteUrl(model.getMinPictureUrl(),model.getServer()));
        return model;
    }

    @Override
    public void addModelInfo(SzModel model) {
        modelDAO.saveInfo(model);
    }

    @Override
    public void modifyInfo(SzModel model) {
        modelDAO.modifyInfo(model);
    }

    public void setUrlGetter(UrlGetter urlGetter) {
        this.urlGetter = urlGetter;
    }
}
